package com.example.common.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.Getter;

import java.util.Arrays;

@Getter
public enum UserRole {
    STUDENT("student"),
    TEACHER("teacher"),
    ADMIN("admin");
//    STUDENT(0, "student"),
//    TEACHER(1, "teacher"),
//    ADMIN(2, "admin");

    @EnumValue
//    public final int value;
    public final String desc;

    //    UserRole(int value, String desc) {
//        this.value = value;
//        this.desc = desc;
//    }
    UserRole(String desc) {
        this.desc = desc;
    }

    // 添加一个静态方法来处理空字符串
    public static UserRole fromString(String text) {
        if (text == null || text.trim().isEmpty()) {
            return null; // 或者返回一个默认值
        }
        return valueOf(text.toUpperCase());
    }

//    public static UserRole of(int value) {
//        switch (value) {
//            case 0:
//                return TEACHER;
//            case 1:
//                return STUDENT;
//            case 2:
//                return ADMIN;
//            default:
////                return null;
////             或者抛出异常：
//                throw new IllegalArgumentException("无效的用户角色值: " + value);
//        }
//    }

    public static String of(int value) {
        switch (value) {
            case 0:
                return TEACHER.getDesc();
            case 1:
                return STUDENT.getDesc();
            case 2:
                return ADMIN.getDesc();
            default:
//                return null;
//             或者抛出异常：
                throw new IllegalArgumentException("无效的用户角色值: " + value);
        }
    }

    // 根据 desc 获取枚举
    public static UserRole of(String desc) {
        return Arrays.stream(values())
                .filter(role -> role.getDesc().equalsIgnoreCase(desc))
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException("无效的角色：" + desc));
    }

    // 根据 desc 获取枚举
    public static UserRole fromDesc(String desc) {
        return Arrays.stream(values())
                .filter(role -> role.getDesc().equalsIgnoreCase(desc))
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException("无效的角色：" + desc));
    }

//    public static String of(String desc) {
//       return desc;
//    }
}
